package com.jiezhuo.controller.jw;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiezhuo.entity.LoginUser;
import com.jiezhuo.entity.jw.*;
import com.jiezhuo.service.jw.JwUserlistService;
import com.jiezhuo.service.jw.JwXqBjkcbzrService;
import com.jiezhuo.service.jw.JwXqJsrkbService;
import com.jiezhuo.service.jw.JwXqKskcService;
import com.jiezhuo.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.SerializationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.jiezhuo
 * @since 2024-06-13
 */
@RestController
@RequestMapping("/jw-xq-bjkcbzr")
@Api(tags = "班级管理")
public class JwXqBjkcbzrController {

    @Autowired
    JwUserlistService jwUserlistService;
    @Autowired
    JwXqKskcService jwXqKskcService;
    @Autowired
    JwXqBjkcbzrService jwXqBjkcbzrService;
    @Autowired
    JwXqJsrkbService jwXqJsrkbService;
    @ApiOperation("添加班级")
    @PostMapping("saveSubject")
    public R saveSubject(@RequestBody JwXqBjkcbzr jwXqBjkcbzr){
//        TODO  判断学科是否存在
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        QueryWrapper<JwXqBjkcbzr> jwXqBjkcbzrQueryWrapper = new QueryWrapper<>();
        jwXqBjkcbzrQueryWrapper.eq(" xq",jwXqBjkcbzr.getXq());
        jwXqBjkcbzrQueryWrapper.eq(" NEW_XQDM",jwXqBjkcbzr.getNewXqdm());
        jwXqBjkcbzrQueryWrapper.eq(" nj",jwXqBjkcbzr.getNj());
        jwXqBjkcbzrQueryWrapper.eq(" BJNO",jwXqBjkcbzr.getBjno());
        jwXqBjkcbzrQueryWrapper.eq(" JXZZFS",jwXqBjkcbzr.getJxzzfs());
        jwXqBjkcbzrQueryWrapper.eq(!StringUtils.isEmpty(jwXqBjkcbzr.getKcId())," KC_ID",jwXqBjkcbzr.getKcId());
        jwXqBjkcbzrQueryWrapper.eq(!StringUtils.isEmpty(jwXqBjkcbzr.getKcxz())," KCXZ",jwXqBjkcbzr.getKcxz());
        List<JwXqBjkcbzr> jwXqBjkcbzrs = jwXqBjkcbzrService.getBaseMapper().selectList(jwXqBjkcbzrQueryWrapper);
        if(jwXqBjkcbzrs.size()>0){
            return R.err().msg("班级已存在");
        }
        String string = UUID.randomUUID().toString();
        jwXqBjkcbzr.setOperator(userId);
        jwXqBjkcbzr.setBjId(string);
        jwXqBjkcbzr.setOperateTime(new Date());
        boolean b = jwXqBjkcbzrService.save(jwXqBjkcbzr);
        return b ?R.ok().data("bjId",string): R.err();
    }
    @ApiOperation("修改班级")
    @PostMapping("updateSubject")
    public R updateSubject(@RequestBody JwXqBjkcbzr jwXqBjkcbzr){
//        TODO  判断学科是否存在
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        QueryWrapper<JwXqBjkcbzr> jwXqBjkcbzrQueryWrapper = new QueryWrapper<>();
        jwXqBjkcbzrQueryWrapper.eq(" xq",jwXqBjkcbzr.getXq());
        jwXqBjkcbzrQueryWrapper.eq(" NEW_XQDM",jwXqBjkcbzr.getNewXqdm());
        jwXqBjkcbzrQueryWrapper.eq(" nj",jwXqBjkcbzr.getNj());
        jwXqBjkcbzrQueryWrapper.eq(" BJNO",jwXqBjkcbzr.getBjno());
        jwXqBjkcbzrQueryWrapper.eq(" JXZZFS",jwXqBjkcbzr.getJxzzfs());
        JwXqBjkcbzr jwXqBjkcbzr1 = jwXqBjkcbzrService.getBaseMapper().selectOne(jwXqBjkcbzrQueryWrapper);
        if(!ObjectUtils.isEmpty(jwXqBjkcbzr1)){
            return R.err().msg("班级已存在");
        }
        jwXqBjkcbzr.setOperator(userId);
        jwXqBjkcbzr.setOperateTime(new Date());
        boolean b = jwXqBjkcbzrService.updateById(jwXqBjkcbzr);
        return b ?R.ok(): R.err();
    }

    @ApiOperation("删除班级")
    @PostMapping("delSubject")
    public R delSubject(@RequestBody JwXqBjkcbzr jwXqBjkcbzr){


        JwXqBjkcbzr byId = jwXqBjkcbzrService.getById(jwXqBjkcbzr.getBjId());
        boolean b=  jwXqBjkcbzrService.delSubject(byId);
        if (b){
           return R.err().msg("班级存在学生");
        }


        int i = jwXqBjkcbzrService.getBaseMapper().deleteById(byId);
        return i>0 ?R.ok(): R.err();
    }

    @ApiOperation("获得班级学科回显")
    @GetMapping("getClassSubject")
    public R getClassSubjectShow(JwVo jwVo){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        jwVo.setUserId(userId);
        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())){
            jwVo.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jwVo.getGzXqdm() + "'");
            if (!contains){
                jwVo.setGzXqdm("'not'");
            }
        }
        ArrayList<String> collect =   jwXqBjkcbzrService.getClassSubjectShow(jwVo);
        return R.ok().data("list",collect);
    }


    @ApiOperation("修改走读班级学科")
    @PostMapping("updateClassSubjectZd")
    public R updateClassSubjectZd(@RequestBody JwXqBjkcbzr jwXqBjkcbzr){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        QueryWrapper<JwXqBjkcbzr> jwXqBjkcbzrQueryWrapper = new QueryWrapper<>();
        jwXqBjkcbzrQueryWrapper.eq("BJNO",jwXqBjkcbzr.getBjno());
        jwXqBjkcbzrQueryWrapper.eq("BJ_ID",jwXqBjkcbzr.getBjId());
        jwXqBjkcbzrQueryWrapper.eq("NEW_XQDM",jwXqBjkcbzr.getNewXqdm());
        jwXqBjkcbzrQueryWrapper.eq("XQ",jwXqBjkcbzr.getXq());
        jwXqBjkcbzrQueryWrapper.eq("NJ",jwXqBjkcbzr.getNj());
        jwXqBjkcbzrQueryWrapper.eq("JXZZFS",jwXqBjkcbzr.getJxzzfs());
        JwXqBjkcbzr jwXqBjkcbzr1 = jwXqBjkcbzrService.getBaseMapper().selectOne(jwXqBjkcbzrQueryWrapper);
        if(!ObjectUtils.isEmpty(jwXqBjkcbzr1) ){
            if(jwXqBjkcbzr.getBjId().equals(jwXqBjkcbzr1.getBjId())){
                boolean b = jwXqBjkcbzrService.updateById(jwXqBjkcbzr);
            }
        }else {
            boolean b = jwXqBjkcbzrService.updateById(jwXqBjkcbzr);
        }
        QueryWrapper<JwXqJsrkb> jwXqJsrkbQueryWrapper = new QueryWrapper<>();
//        jwXqJsrkbQueryWrapper.eq("BJNO",jwXqBjkcbzr.getBjno());
        jwXqJsrkbQueryWrapper.eq("BJ_ID",jwXqBjkcbzr.getBjId());
        jwXqJsrkbQueryWrapper.eq("NEW_XQDM",jwXqBjkcbzr.getNewXqdm());
        jwXqJsrkbQueryWrapper.eq("XQ",jwXqBjkcbzr.getXq());
//        jwXqJsrkbQueryWrapper.eq("NJ",jwXqBjkcbzr.getNj());
        jwXqJsrkbQueryWrapper.eq("JXZZFS",jwXqBjkcbzr.getJxzzfs());
        jwXqJsrkbQueryWrapper.eq("KC_ID",jwXqBjkcbzr.getKcId());
        jwXqJsrkbQueryWrapper.eq("KCXZ",jwXqBjkcbzr.getKcxz());

        JwXqJsrkb jwXqJsrkb = jwXqJsrkbService.getBaseMapper().selectOne(jwXqJsrkbQueryWrapper);
        if(!StringUtils.isEmpty(jwXqJsrkb) ){
            if(jwXqJsrkb.getId().equals(jwXqJsrkb.getId())){
                jwXqJsrkb.setRjz(jwXqBjkcbzr.getBzr());
                jwXqJsrkb.setKcId(jwXqBjkcbzr.getKcId());
                jwXqJsrkb.setKcxz(jwXqBjkcbzr.getKcxz());
                boolean b = jwXqJsrkbService.updateById(jwXqJsrkb);
            }
        }else {
            jwXqJsrkb=new JwXqJsrkb();
            jwXqJsrkb.setBjId(jwXqBjkcbzr.getBjId());
            jwXqJsrkb.setNewXqdm(jwXqBjkcbzr.getNewXqdm());
            jwXqJsrkb.setXq(jwXqBjkcbzr.getXq());
            jwXqJsrkb.setRjz(jwXqBjkcbzr.getBzr());
            jwXqJsrkb.setKcId(jwXqBjkcbzr.getKcId());
            jwXqJsrkb.setKcxz(jwXqBjkcbzr.getKcxz());
            jwXqJsrkb.setJxzzfs(jwXqBjkcbzr.getJxzzfs());
            jwXqJsrkb.setOperator(userId);
            jwXqJsrkb.setOperateTime(new Date());
            int i = jwXqJsrkbService.getBaseMapper().insert(jwXqJsrkb);
        }
        return  R.ok();
    }

    @ApiOperation("删除走读班")
    @PostMapping("delZdTeacherSubject")
    public R delZdTeacherSubject(@RequestBody JwXqBjkcbzr jwXqBjkcbzr){
//        QueryWrapper<JwXqBjkcbzr> jwXqBjkcbzrQueryWrapper = new QueryWrapper<>();
//        jwXqBjkcbzrQueryWrapper.eq("BJNO",jwXqBjkcbzr.getBjno());
//        jwXqBjkcbzrQueryWrapper.eq("BJID",jwXqBjkcbzr.getBjId());
//        jwXqBjkcbzrQueryWrapper.eq("NEW_XQDM",jwXqBjkcbzr.getNewXqdm());
//        jwXqBjkcbzrQueryWrapper.eq("XQ",jwXqBjkcbzr.getXq());
//        jwXqBjkcbzrQueryWrapper.eq("NJ",jwXqBjkcbzr.getNj());
//        jwXqBjkcbzrQueryWrapper.eq("JXZZFS",jwXqBjkcbzr.getJxzzfs());
//        jwXqBjkcbzrQueryWrapper.eq("JXZZFS",jwXqBjkcbzr.getJxzzfs());
//        jwXqBjkcbzrQueryWrapper.eq("KC_ID",jwXqBjkcbzr.getKcId());
//        jwXqBjkcbzrQueryWrapper.eq("KCXZ",jwXqBjkcbzr.getKcxz());
        QueryWrapper<JwXqJsrkb> jwXqJsrkbQueryWrapper = new QueryWrapper<>();
//        jwXqJsrkbQueryWrapper.eq("BJNO",jwXqBjkcbzr.getBjno());
        jwXqJsrkbQueryWrapper.eq("BJ_ID",jwXqBjkcbzr.getBjId());
        jwXqJsrkbQueryWrapper.eq("NEW_XQDM",jwXqBjkcbzr.getNewXqdm());
        jwXqJsrkbQueryWrapper.eq("XQ",jwXqBjkcbzr.getXq());
//        jwXqJsrkbQueryWrapper.eq("NJ",jwXqBjkcbzr.getNj());
        jwXqJsrkbQueryWrapper.eq("JXZZFS",jwXqBjkcbzr.getJxzzfs());
        jwXqJsrkbQueryWrapper.eq("KC_ID",jwXqBjkcbzr.getKcId());
        jwXqJsrkbQueryWrapper.eq("KCXZ",jwXqBjkcbzr.getKcxz());
        JwXqJsrkb jwXqJsrkb = jwXqJsrkbService.getBaseMapper().selectOne(jwXqJsrkbQueryWrapper);
        int i = jwXqJsrkbService.getBaseMapper().deleteById(jwXqJsrkb.getId());

        i = jwXqBjkcbzrService.getBaseMapper().deleteById(jwXqJsrkb.getBjId());


        return i>0? R.ok(): R.err();
    }


}

